package com.xyy.saas.payment.adapter;

import com.xyy.saas.payment.adapter.channel.AliCommonAdapter;
import com.xyy.saas.payment.adapter.channel.WxCommonAdapter;
import com.xyy.saas.payment.adapter.channel.fm.FuminAdapter;
import com.xyy.saas.payment.common.constants.ChannelConstants;
import com.xyy.saas.payment.cores.bo.OpenPayBo;
import com.xyy.saas.payment.cores.bo.QueryCreateAllPayBo;
import com.xyy.saas.payment.cores.enums.ChannelEnum;
import com.xyy.saas.payment.exception.PaymentException;
import com.xyy.saas.payment.pojo.dto.OpenPayDto;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class RouteManager implements InitializingBean {

    private Map<String, PaymentAdapter> adapters = new HashMap<>();

    @Autowired
    private AliCommonAdapter aliCommonAdapter;
    @Autowired
    private WxCommonAdapter wxCommonAdapter;
    @Autowired
    private FuminAdapter fuminAdapter;

    @Override
    public void afterPropertiesSet() {
        adapters.put(ChannelConstants.CHANNEL_FUMIN_BANK_NAME, fuminAdapter);
        adapters.put(ChannelConstants.CHANNEL_DIRECT_NAME_ALIPAY, aliCommonAdapter);
        adapters.put(ChannelConstants.CHANNEL_DIRECT_NAME_WX, wxCommonAdapter);
    }

    public String getRoute(String payChannel) {
        if(payChannel.contains(ChannelConstants.CHANNEL_FUMIN_BANK_NAME)) {
            return ChannelConstants.CHANNEL_FUMIN_BANK_NAME;
        }
        if(payChannel.contains(ChannelConstants.CHANNEL_DIRECT_NAME)) {
            if(payChannel.contains(ChannelEnum.WX.getCode())) {
                return ChannelConstants.CHANNEL_DIRECT_NAME_WX;
            }
            if(payChannel.contains(ChannelEnum.ALIPAY.getCode())) {
                return ChannelConstants.CHANNEL_DIRECT_NAME_ALIPAY;
            }
            return ChannelConstants.CHANNEL_AGGREGATE_NAME;
        } else {
            return payChannel;
        }
    }

    public PaymentAdapter getAdapter(String channel) {
       return adapters.get(getRoute(channel));
    }

    /**
     * 进件状态查询
     * @param accountId
     * @return
     * @throws PaymentException
     */
    public QueryCreateAllPayBo queryCreateAllPay(String channel, String accountId, String businessIdType) throws PaymentException {
        return getAdapter(channel).queryCreateAllPay(accountId, businessIdType);
    }

    /**
     * 开通支付状态查询
     * @return
     * @throws PaymentException
     */
    public Map<String, Boolean> queryOpenPay(String channel, String accountId, String businessIdType) throws PaymentException {
        return getAdapter(channel).queryOpenPay(accountId, businessIdType);
    }

    /**
     * 开通支付功能
     * @param openPayDto
     * @return
     * @throws PaymentException
     */
    public OpenPayBo openPay(String channel, OpenPayDto openPayDto) throws PaymentException {
        return getAdapter(channel).openPay(openPayDto);
    }

}
